<!DOCTYPE html>
<html>
<head>
    <title>微信公众号管理平台</title>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
    <link rel="stylesheet" href="style.css"/>
    <script type="text/javascript" src="jquery.js"></script>
</head>
<body>
    <!--添加弹框-->
    <div class="addTan">
        <div class="midDivs">
            <div>
                <label>添加至站点：</label>
                <input type="text" onblur="valShow1(this)"/>
            </div>
            <div>
                <label>&nbsp;&nbsp;&nbsp;添加至省：</label>
                <input type="text" onblur="valShow2(this)"/>
            </div>
            <div>
                <label>&nbsp;&nbsp;&nbsp;添加至市：</label>
                <input type="text" onblur="valShow3(this)"/>
            </div>
            <div class="fontColor">(* 只能添加一个)</div>
            <div class="butns">
                <button onclick="subContents(this)">提交</button><button onclick="closeAdd(this)">取消</button>
            </div>
        </div>
    </div>
    <!--添加弹框-->
     <div class="allContnts">
        <div class="weiTitle"></div>
        <div class="middleContents">
            <div class="leftDiv">
                <ul>
                    <li class="bgColor">
                        <img src="images/weixinChoose.png" alt="" class="img1"/>
                        <span>公众号</span>
                    </li>
                    <li>
                        <img src="images/data1.png" alt="" class="img2"/>
                        <span>数据</span>
                    </li>
                </ul>
            </div>
            <div class="rightDiv">
                <!--公众号-->
                <div class="gongZhong">
                    <div class="searchFor">
                        <input type="text" class="inpText" placeholder="搜索一下"/>
                        <img src="images/searchImg.png" alt="" onclick="searchCons()"/>
                    </div>
                    <div class="clear"></div>
                    <div class="conShow">
                        <ul class="listUl"></ul>
						<div class="pageDiv"></div>
                    </div>
                </div>
                <!--数据-->
                <div class="dataWei" style="display: none;">
                    <div class="dataWeiShow">

                    </div>
                </div>
            </div>
        </div>
     </div>
</body>
<script type="text/javascript">

    $(".leftDiv ul li").click(function(){
        $(this).addClass("bgColor").siblings().removeClass("bgColor");
        if($(this).first().find("span").html() == "公众号"){
            $(".img1").attr("src","images/weixinChoose.png");
            $(".img2").attr("src","images/data1.png");
            $(".gongZhong").show();
            $(".dataWei").hide();
        }
        if($(this).first().find("span").html() == "数据"){
            $(".img1").attr("src","images/weixin1.png");
            $(".img2").attr("src","images/dataChoose.png");
            $(".gongZhong").hide();
            $(".dataWei").show();
            $("table").show();
            $.ajax({
                type:"get",
                url:"/siteList",
                data:{"":""},
                dataType:"json",
                success:function(data){
                    var dataWeiShow = $(".dataWeiShow");
                     var datas = data['data'];
                    var html="<table id='tableId'>";
                        html+="<tr>";
                        html+="<th>站点名称</th>";
                        html+="<th>省份</th>";
                        html+="<th>市辖</th>";
                        html+="<th>站点</th>";
                        html+="<td>操作</td>"
                        html+="</tr>";
                    //增加国家
                    html+="<tr>";
                    html+="<td>国家</td>";
                    html+="<td type='1' value='' >国家</td>";
                    html+="<td type='2' value=''>国家</td>";
                    html+="<td type='3' value='0' onclick='showFind(this)'>0</td>";
                    html+="<td></td>"
                    html+="</tr>";

                    for(var j = 0;j<datas.length;j++){

                        var dataAll = datas[j];
                        console.log("dataAll="+dataAll);

                        html+="<tr>";
                        html+="<td>"+dataAll['siteName']+"</td>";
                        html+="<td type='1' value='"+dataAll['province']+"' onclick='showFind(this)'>"+dataAll['province']+"</td>";
                        html+="<td type='2' value='"+dataAll['city']+"' onclick='showFind(this)'>"+dataAll['city']+"</td>";
                        html+="<td type='3' value='"+dataAll['siteId']+"' onclick='showFind(this)'>"+dataAll['siteId']+"</td>";
                        html+="<td><a href='/spider?siteId="+dataAll['siteId']+"' target='_blank'>抓取</a></td>";
                        html+="</tr>";
                    }
                    html+="</table>";
                    dataWeiShow.html(html);

                },error:function(){
                    alert("数据获取失败！")
                }
            })



        }
    });


	// 展示查询数据
    function showFind(obj){
        var types =  $(obj).attr("type");
        var values =  $(obj).attr("value");
        $("table").hide();
        $.ajax({
            type:"post",
            url:"/mpList",
            data:{"type":types,"value":values},
            dataType:"json",
            success:function(data){
                var dataWeiShow = $(".dataWeiShow");
                var dataAll = data['data'];
                var html= '<ul>';
                for(var j = 0;j<dataAll.length;j++){
                    var kk = dataAll[j];
                    html+='<li>';
                    html+='<div class="leftLis">';
                    html+='<img src="/timg.jpg" alt=""/>';
                    html+='<div class="rightText">';
                    html+='<span>'+kk['name']+'</span><br>';
                    html+='<span>微信号：'+kk['code']+'</span>';
                    html+='</div>';
                    html+='</div>';
                    html+='<div class="rightLis">';
                    html+='<button onclick="lookDatas(this)">查看数据</button>';
                    html+='<button style="display: none;" onclick="shouDatas(this)">收起</button>';
                    html+='<button onclick="del(this,\'';
                    html+=kk['code'];
                    html+='\')">删除</button>';
                    html+='</div>';
                    html+='<div class="clear"></div>';
                    html+='<div class="gongGao"></div>';
                    html+='</li>';
                }
                    html+='</ul>';
                    dataWeiShow.append(html);
                },error:function(){
                      alert("获取失败!");
                }
        });
    }



	
    var key = "";
	var allPage = 1;
    // 搜索
     function searchCons(){
         var inpText = $(".inpText").val();
		 key=inpText;
         var conShow = $(".conShow");
         if(inpText == ""){
            alert("请输入搜索内容");
         }else{
             $.ajax({
                 type:"post",
                 url:"/search",
                 data:{"keyword":inpText,"page":1},
                 dataType:"json",
                 success:function(data){
                     var dataAll = data['data'];
                     var conShow = $(".conShow");
                     var html ='';
                     for(i=0;i<dataAll.length;i++){
                         var datas = dataAll[i];
                         html+='<li>';
                         html+='<div class="leftLis"><img src="/timg.jpg"/>';
                         html+='<div class="rightText"> ';
                         html+='<span>'+datas['name']+'</span><br> ';
                         html+='<span>微信号:<b>'+datas['code']+'</b></span> ';
                         html+='</div>';
                         html+='</div>';
                         html+='<div class="rightLis">';
                         html+='<button onclick="addDatas(this)">添加</button><button onclick="lookAt(this)"><a target="_blank"  href="#">查看</a></button>';
                         html+='</div>';
                         html+='</li>';

                     }
                     conShow.find("ul:eq(0)").html(html);
					 
					 var pageAll = data['maxpage'];
					 allPage = pageAll; 
					 if(pageAll>=2){
						 var html2 = '<ul class="pageUls">';
							 html2+='<li onclick="pageShang()" id="before">上一页</li>';
						 for(var pp = 1;pp<=pageAll;pp++){
							 html2+='<li onclick="pageFen(this)" page="'+pp+'">'+pp+'</li>';						
						 }
						 html2+='<li onclick="nextPage()" id="next">下一页</li>';
						 html2+='<li>共'+pageAll+'页</li>';
						 html2+='</ul>';
					}				
				    $(".pageDiv").html(html2);	
				   	$("#before").next().attr("page","1").addClass("colorChange");
                 }
             });
             $(".inpText").val("");
         }
     }
	 
		 
	function pageFen(obj){
	    var thisNumber = $(obj).attr("page");
		$(obj).addClass("colorChange").siblings().removeClass("colorChange");
		page(thisNumber);
	}
	 var dangQian = 1;	 
	 // 点击分页
	 function page(thisNumber){
		 dangQian=thisNumber;		 
		 $.ajax({
			 type:"post",
			 url:"/search",
			 data:{"keyword":key,"page":thisNumber},
			 dataType:"json",
			 success:function(data){
					 var dataAll = data['data'];
                     var conShow = $(".conShow");
                     var html ='';
                     for(i=0;i<dataAll.length;i++){
                         var datas = dataAll[i];
                         html+='<li>';
                         html+='<div class="leftLis"><img src="/timg.jpg"/>';
                         html+='<div class="rightText"> ';
                         html+='<span>'+datas['name']+'</span><br> ';
                         html+='<span>微信号:<b>'+datas['code']+'</b></span> ';
                         html+='</div>';
                         html+='</div>';
                         html+='<div class="rightLis">';
                         html+='<button onclick="addDatas(this)">添加</button><button onclick="lookAt(this)"><a target="_blank" href="#">查看</a></button>';
                         html+='</div>';
                         html+='</li>';

                     }
                     conShow.find("ul:eq(0)").html(html); 
			}
		});
	 
	 }
	 	 
	 // 上一页
	 
	 function pageShang(){
	   if(dangQian >1){
	    var cur = dangQian-1;
		$("li[page='"+dangQian+"']").removeClass("colorChange");
		$("li[page='"+cur+"']").addClass("colorChange");
	 	page(cur);
		}
	 }
	 
	// 下一页
     function nextPage(){
	    if(dangQian<allPage){
			var next = dangQian+1;
			$("li[page='"+dangQian+"']").removeClass("colorChange");
			$("li[page='"+next+"']").addClass("colorChange");
			page(next);				
		}
	 
	 }	
	 	 
	
    // 添加公众号
    var nameG;
    var codeG;
    var addBtn;
    function addDatas(obj){
        addBtn = $(obj);
        var name = $(obj).parent().parent().find("span:eq(0)").html();
        nameG = name;
        var code = $(obj).parent().parent().find("b").html();
        codeG = code;
        $(".addTan").fadeIn();
    }

    function valShow1(obj){
        var tt = /^\d+$/g;
         if($(obj).val().length == 0){
             $(obj).parents(".addTan").find("input:eq(1)").removeAttr("disabled","disabled");
             $(obj).parents(".addTan").find("input:eq(2)").removeAttr("disabled","disabled");
         }else if(tt.test($(obj).val())){
             $(obj).parents(".addTan").find("input:eq(1)").attr("disabled","disabled");
             $(obj).parents(".addTan").find("input:eq(2)").attr("disabled","disabled");
         }else{
             alert("站点类型为正整数！");
             $(obj).parents(".addTan").find("input:eq(1)").attr("disabled","disabled");
             $(obj).parents(".addTan").find("input:eq(2)").attr("disabled","disabled");
         }
     }
    function valShow2(obj){
        if($(obj).val().length == 0){
            $(obj).parents(".addTan").find("input:eq(0)").removeAttr("disabled","disabled");
            $(obj).parents(".addTan").find("input:eq(2)").removeAttr("disabled","disabled");
        }else{
            $(obj).parents(".addTan").find("input:eq(0)").attr("disabled","disabled");
            $(obj).parents(".addTan").find("input:eq(2)").attr("disabled","disabled");
        }
    }
    function valShow3(obj){
        if($(obj).val().length == 0){
            $(obj).parents(".addTan").find("input:eq(0)").removeAttr("disabled","disabled");
            $(obj).parents(".addTan").find("input:eq(1)").removeAttr("disabled","disabled");
        }else{
            $(obj).parents(".addTan").find("input:eq(0)").attr("disabled","disabled");
            $(obj).parents(".addTan").find("input:eq(1)").attr("disabled","disabled");
        }
    }

    //提交添加内容
    function subContents(obj){
        // 站点
        var siteIdInp = $(obj).parents(".addTan").find("input:eq(0)").val();
        // 省
        var provinceInp = $(obj).parents(".addTan").find("input:eq(1)").val();
        // 市
        var cityInp = $(obj).parents(".addTan").find("input:eq(2)").val();

            $.ajax({
                type:"post",
                url:"/add",
                data:{"name":nameG,"code":codeG,"siteId":siteIdInp,"province":provinceInp,"city":cityInp},
                dataType:"json",
                success:function(obj){
                    var code = obj["code"];
                    var msg = obj["message"];
                    if(code==0) {
                        addBtn.html("已添加");
                        addBtn.attr("disabled", "disabled");
                    }else{
                        alert(msg);
                    }
                },error:function(){
                    alert("添加失败！");
                }
            });

            $(obj).parents(".addTan").find("input:eq(0)").val("");
            $(obj).parents(".addTan").find("input:eq(1)").val("");
            $(obj).parents(".addTan").find("input:eq(2)").val("");
            $(obj).parents(".addTan").find("input:eq(0)").removeAttr("disabled","disabled");
            $(obj).parents(".addTan").find("input:eq(1)").removeAttr("disabled","disabled");
            $(obj).parents(".addTan").find("input:eq(2)").removeAttr("disabled","disabled");
            $(obj).parents(".addTan").fadeOut();
    }


    //隐藏弹框
    function closeAdd(obj){
        $(obj).parents(".addTan").find("input:eq(0)").val("");
        $(obj).parents(".addTan").find("input:eq(1)").val("");
        $(obj).parents(".addTan").find("input:eq(2)").val("");
        $(obj).parents(".addTan").find("input:eq(0)").removeAttr("disabled","disabled");
        $(obj).parents(".addTan").find("input:eq(1)").removeAttr("disabled","disabled");
        $(obj).parents(".addTan").find("input:eq(2)").removeAttr("disabled","disabled");
        $(obj).parents(".addTan").fadeOut();
    }


   // 查看
    function lookAt(obj){
	    var name = $(obj).parent().parent().find("span:eq(0)").html();
		$(obj).find("a").attr("href","http://weixin.sogou.com/weixin?type=1&query="+name+"");
		var ss = $(obj).find("a").attr("href")
	}

    //删除
    function del(obj,code){

        $.ajax({
            type:"post",
            url:"/del",
            data:{"code":code},
            dataType:"json",
            success:function(data){
                code = data['code'];
                if(code==0){
                    $(obj).parent().parent().hide();
                }else{
                    alert("删除失败")
                }

            }
        });

    }

    // 新闻列表
   function lookDatas(obj){
       $(obj).hide();
       $(obj).parent().find("button:eq(1)").show();
        var gongGao = $(obj).parent().parent().first().find("div[class='gongGao']");
        var firstSpan = $(obj).parent().parent().first().find("span").html();
        if(gongGao.html() == ""){
            $.ajax({
                type:"post",
                url:"/getNews",
                data:{"name":firstSpan},
                dataType:"json",
                success:function(data){
                    var dataAll = data['data'];
                    var gongGao = $(obj).parent().parent().first().find("div[class='gongGao']");
                    var html ="<div>";
                    var aa =1;
                    for(var a = 0;a<dataAll.length;a++){
                        var datas = dataAll[a];
                        html+="<p class='div'>"+aa+".&nbsp;<a target='_blank' href='"+datas['url']+"'>"+datas['title']+"</a>&nbsp;&nbsp;&nbsp;&nbsp;" +datas['newsTime']+ "</p>";
                        aa++;
                    }
                    html+="</div>";
                    gongGao.append(html);
                    gongGao.show();
                }
            });
        }else{
            gongGao.show();

        }
    }

   function shouDatas(obj){
       $(obj).parent().find("button:eq(0)").show();
       $(obj).hide();
       var gongGao = $(obj).parent().parent().first().find("div[class='gongGao']");
       gongGao.hide();
   }



</script>
</html>
